import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/Layout/header.vue'
import { useUserStore } from '@/store/user'

export const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('../views/login/login.vue'),
  },
  {
    path: '/index',
    component: Layout,
    redirect: '/index',
    children: [
      {
        name: 'index',
        path: '/index',
        component: () => import('../views/homepage/index.vue'),
      },
    ],
  },
  {
    path: '/home',
    component: Layout,
    redirect: '/home',
    meta: { hideElement: true },
    children: [
      {
        name: 'home',
        path: '/home',
        component: () => import('../views/homepage/home.vue'),
      },
    ],
  },
  {
    path: '/after_class',
    component: Layout,
    redirect: '/after_class',
    meta: { hideElement: false },
    children: [
      {
        name: 'after_class',
        path: '/after_class',
        component: () => import('../views/after/after_class.vue'),
      },
    ],
  },
  {
    path: '/subject_four',
    component: Layout,
    redirect: '/subject_four',
    meta: { hideElement: false },
    children: [
      {
        name: 'subject_four',
        path: '/subject_four',
        component: () => import('../views/after/subject_four.vue'),
      },
    ],
  },
  {
    path: '/wrong_question_collection',
    component: Layout,
    redirect: '/wrong_question_collection',
    meta: { hideElement: false },
    children: [
      {
        name: 'wrong_question_collection',
        path: '/wrong_question_collection',
        component: () => import('../views/after/wrong_question_collection.vue'),
      },
    ],
  },
  {
    path: '/specialExercises',
    component: Layout,
    redirect: '/specialExercises',
    meta: { hideElement: false },
    children: [
      {
        name: 'specialExercises',
        path: '/specialExercises',
        component: () => import('../views/specialExercises/index.vue'),
      },
      {
        name: 'specialExam',
        path: '/specialExam',
        component: () => import('../views/specialExercises/specialExam.vue'),
      },
    ],
  },
  {
    path: '/three_force_test',
    component: Layout,
    redirect: '/three_force_test',
    meta: { hideElement: false },
    children: [
      {
        name: 'three_force_test',
        path: '/three_force_test',
        component: () => import('../views/after/three_force_test.vue'),
      },
    ],
  },
  {
    path: '/mnks',
    component: Layout,
    redirect: '/mnks',
    meta: { hideElement: false },
    children: [
      {
        name: 'mnks',
        path: '/mnks',
        component: () => import('../views/question/mnks.vue'),
      },
    ],
  },
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting',
    meta: { hideElement: false },
    children: [
      {
        name: 'setting',
        path: '/setting',
        component: () => import('../views/setting/index.vue'),
      },
    ],
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order',
    meta: { hideElement: false },
    children: [
      {
        name: 'order',
        path: '/order',
        component: () => import('../views/order/index.vue'),
      },
    ],
  },
  {
    path: '/after',
    component: Layout,
    redirect: '/after',
    meta: { hideElement: false },
    children: [
      {
        name: 'score_ranking',
        path: '/score_ranking',
        component: () => import('../views/after/score_ranking.vue'),
      },
    ],
  },
  {
    path: '/after',
    component: Layout,
    redirect: '/after',
    meta: { hideElement: false },
    children: [
      {
        name: 'knowledge',
        path: '/knowledge',
        component: () => import('../views/after/knowledge.vue'),
      },
    ],
  },
  {
    path: '/after',
    component: Layout,
    redirect: '/after',
    meta: { hideElement: false },
    children: [
      {
        name: 'iconTrick',
        path: '/iconTrick',
        component: () => import('../views/after/icon_trick.vue'),
      },
    ],
  },
  {
    path: '/wrongCollection',
    component: Layout,
    redirect: '/wrongCollection',
    meta: { hideElement: false },
    children: [
      {
        name: 'wrong',
        path: '/wrong',
        component: () => import('../views/wrongCollection/wrong.vue'),
      },
      {
        name: 'collection',
        path: '/collection',
        component: () => import('../views/wrongCollection/collection.vue'),
      },
    ],
  },
  {
    path: '/mnExam',
    component: Layout,
    redirect: '/mnExam',
    meta: { hideElement: false },
    children: [
      {
        name: 'mnExam',
        path: '/mnExam',
        component: () => import('../views/question/index.vue'),
      },
    ],
  },
  {
    path: '/realExam',
    // component: Layout,
    meta: { hideElement: false },
    children: [
      {
        name: 'index',
        path: 'index',
        component: () => import('../views/realExam/index.vue'),
      },
      {
        name: 'exam',
        path: 'exam',
        component: () => import('../views/realExam/exam.vue'),
      },
    ],
  },
  {
    path: '/carefullyChosen',
    component: Layout,
    redirect: '/carefullyChosen',
    meta: { hideElement: false },
    children: [
      {
        name: 'carefullyChosen',
        path: '/carefullyChosen',
        component: () => import('../views/carefullyChosen/index.vue'),
      },
      {
        name: 'carefullyExam',
        path: '/carefullyExam',
        component: () => import('../views/carefullyChosen/carefullyExam.vue'),
      },
    ],
  },
  {
    path: '/chapterExercises',
    component: Layout,
    redirect: '/chapterExercises',
    meta: { hideElement: false },
    children: [
      {
        name: 'chapterExercises',
        path: '/chapterExercises',
        component: () => import('../views/chapterExercises/index.vue'),
      },
      {
        name: 'chapterExam',
        path: '/chapterExam',
        component: () => import('../views/chapterExercises/chapterExam.vue'),
      },
    ],
  },
  {
    path: '/confidentialRoll',
    component: Layout,
    redirect: '/confidentialRoll',
    meta: { hideElement: false },
    children: [
      {
        name: 'confidentialRoll',
        path: '/confidentialRoll',
        component: () => import('../views/confidentialRoll/index.vue'),
      },
      {
        name: 'confidentialExam',
        path: '/confidentialExam',
        component: () => import('../views/confidentialRoll/confidentialExam.vue'),
      },
    ],
  },
  {
    path: '/wrongReview',
    component: Layout,
    redirect: '/wrongReview',
    meta: { hideElement: false },
    children: [
      {
        name: 'wrongReview',
        path: '/wrongReview',
        component: () => import('../views/wrongReview/index.vue'),
      },
      {
        name: 'wrongExam',
        path: '/wrongExam',
        component: () => import('../views/wrongReview/wrongExam.vue'),
      }
    ],
  },
  {
    path: '/wrongCheck',
    component: Layout,
    redirect: '/wrongCheck',
    meta: { hideElement: false },
    children: [
      {
        name: 'wrongCheck',
        path: '/wrongCheck',
        component: () => import('../views/question/wrongCheck.vue'),
      }
    ],
  },
]
const router = createRouter({
  scrollBehavior: () => ({ left: 0, top: 0 }),
  history: createWebHistory('/pc'),
  routes,
})
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  // 允许直接访问登录页
  if (to.path === '/login') {
    next()
    return
  }
  // 没有 accessToken，跳转到登录页
  if (!userStore.accessToken) {
    next({ path: '/login' })
    return
  }
  next()
})
export default router
